package com.atguigu.gmall.product.rpc;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.*;
import com.atguigu.gmall.product.service.SkuImageService;
import com.atguigu.gmall.product.service.SkuInfoService;
import com.atguigu.gmall.product.service.SkuSaleAttrValueService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/api/inner/rpc/product")
public class SkuRpcController {
    @Autowired
    SkuInfoService skuInfoService;

    @Autowired
    SkuImageService skuImageService;

    @Autowired
    SpuSaleAttrService spuSaleAttrService;

    @Autowired
    SkuSaleAttrValueService skuSaleAttrValueService;

    /**
     * 1、获取skuInfo
     * @param skuId
     * @return
     */
    @GetMapping("skuInfo/{skuId}")
    public Result<SkuInfo> getSkuInfo(@PathVariable("skuId") Long skuId) {
        SkuInfo skuInfo = skuInfoService.getById(skuId);
        return Result.ok(skuInfo);
    }

    /**
     * 2、获取sku图片
     * @param skuId
     * @return
     */
    @GetMapping("skuImages/{skuId}")
    public Result<List<SkuImage>> getSkuImages(@PathVariable("skuId") Long skuId) {
        List<SkuImage> list = skuImageService.lambdaQuery().eq(SkuImage::getSkuId, skuId).list();
        return Result.ok(list);
    }

    /**
     * 3、获取实时价格
     * @param skuId
     * @return
     */
    @GetMapping("skuPrice/{skuId}")
    public Result<BigDecimal> getSkuPrice(@PathVariable("skuId") Long skuId) {
        SkuInfo one = skuInfoService.lambdaQuery()
                .select(SkuInfo::getPrice) //只查找指定列
                .eq(SkuInfo::getId, skuId).one();
        return Result.ok(one.getPrice());

    }

    /**
     * 4、获取sku对应spu的销售属性和值
     * @param spuId
     * @return
     */
    @GetMapping("spuSleAttr/{spuId}/{skuId}")
    public Result<List<SpuSaleAttr>> getSpuSaleAttr (@PathVariable("spuId") Long spuId, @PathVariable("skuId") Long skuId) {
        List<SpuSaleAttr> list = spuSaleAttrService.getSpuSaleAttrListOrder(spuId,skuId);
        return Result.ok(list);
    }

    /**
     * 5、获取sku对应的valueSkuJson,兄弟sku
     * @param spuId
     * @return
     */
    @GetMapping("valueSkuJson/{spuId}")
    public Result<String> getValueSkuJson(@PathVariable("spuId") Long spuId) {
        String json = skuSaleAttrValueService.getValueSkuJson(spuId);
        return Result.ok(json);
    }

}
